<template>
	<view style="padding: 30rpx;">
		<button @click="callSetStorage()">setStorage</button>
		<button @click="callGetStorage()">获取缓存</button>
		<button @click="callStoInfo()">获取字段列表</button>
		<button @click="callRemove()" type="warn" plain="true">删除</button>
		<button @click="callClear()" type="warn">清空</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {}
		},
		methods: {
			callSetStorage() {
				console.log("callSetStorage--> userName YXP");
				uni.setStorage({
					key: "userName",
					data: "YXP",
					success() {
						console.log("存储userName 成功回调");
					},
					fail() {
						console.error("存储userName 失败");
					}
				})
			},
			callGetStorage() {
				uni.getStorage({
					key: "userName",
					success(result) {
						console.log("获取本地缓存成功 result.data:" + result.data);
					},
					fail(result) {
						console.error("获取本地缓存失败:" + JSON.stringify(result));
						console.error("获取本地缓存失败:" + result.errMsg);
					}
				})
			},
			callStoInfo() {
				console.log("callStoInfo-->");
				uni.getStorageInfo({
					success(result) {
						console.log("缓存信息:" + JSON.stringify(result));
						console.log("缓存键列表:" + result.keys);
					}
				})
			},
			callRemove() {
				uni.removeStorage({
					key: "userName",
					success() {
						console.log("删除userName缓存成功");
					},
					fail() {
						console.error("删除userName缓存失败");
					}
				})
			},
			callClear() {
				uni.clearStorage({
					success() {
						console.log("所有缓存已清空");
					},
					fail() {
						console.error("清空缓存失败");
					}
				});
			}
		}
	}
</script>

<style>
	button {
		margin: 30rpx;
	}
</style>